//
//  ViewController.m
//  newTestGit
//
//  Created by Hongshi on 15/10/26.
//  Copyright © 2015年 Hongshi. All rights reserved.
//

#import "ViewController.h"
#import "ZBarSDK.h"
#import "QRCodeGenerator.h"
#import "Masonry.h"
#import "ALView+PureLayout.h"
#import "newController.h"
#import "testViewController.h"
#import "MapViewController.h"
#import "GesturesToLockController.h"

#define URLPath @"https://api.douban.com/v2/book/1220562"


@interface ViewController ()<ZBarReaderDelegate>
{
    UILabel *resultText;
    UIImageView *resultImage;
    UITextField *resultField;
    UIButton *btn;
    UIButton *btn1;
    UIButton *btn2;
    UIButton *btn3;
    UIButton *btn4;
    UIButton *btn5;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self presentOtherController];//手势密码
    
    self.navigationItem.title = @"首页";
    self.view.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:0.502 alpha:1.000];
    
    resultField = [[UITextField alloc] initWithFrame:CGRectZero];
    resultField.placeholder = @"输入需要生成二维码的字符串";
    resultField.backgroundColor = [UIColor whiteColor];
    resultField.borderStyle = UITextBorderStyleRoundedRect;
    [self.view addSubview:resultField];
    
    resultImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    resultImage.backgroundColor = [UIColor purpleColor];
    [self.view addSubview:resultImage];
    
    btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectZero ;
    btn.backgroundColor = [UIColor redColor];
    [btn setTitle:@"扫描" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(press:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn1.frame = CGRectZero ;
    [btn1 setTitle:@"生成二维码" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn1.backgroundColor = [UIColor colorWithWhite:0.298 alpha:1.000];
    [btn1 addTarget:self action:@selector(pressAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn1];
    
    btn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn2.frame = CGRectZero ;
    btn2.tag = 201;
    [btn2 setTitle:@"旋转木马" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn2.backgroundColor = [UIColor colorWithRed:0.800 green:0.400 blue:1.000 alpha:1.000];
    [btn2 addTarget:self action:@selector(pressAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn2];
    
    btn3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn3.frame = CGRectZero ;
    btn3.tag = 202;
    [btn3 setTitle:@"启动动画" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn3.backgroundColor = [UIColor colorWithWhite:0.502 alpha:1.000];
    [btn3 addTarget:self action:@selector(pressAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn3];
    
    btn4 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn4.frame = CGRectZero ;
    btn4.tag = 203;
    [btn4 setTitle:@"地图界面" forState:UIControlStateNormal];
    [btn4 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn4.backgroundColor = [UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000];
    [btn4 addTarget:self action:@selector(pressAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn4];
    
    btn5 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn5.frame = CGRectZero ;
    btn5.tag = 204;
    [btn5 setTitle:@"手势密码" forState:UIControlStateNormal];
    [btn5 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn5.backgroundColor = [UIColor colorWithRed:1.000 green:0.400 blue:0.400 alpha:1.000];
    [btn5 addTarget:self action:@selector(pressAction1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn5];

}

- (void) viewWillAppear:(BOOL)animated {

    HttpClient *client = [HttpClient shareClient];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:3];
    [client postPath:URLPath params:param resultBlock:^(id responseObjec, NSError *error) {
        NSLog(@"responseObjec_Content = %@",responseObjec);
    }];
}

- (void) presentOtherController {
    GesturesToLockController *VC = [[GesturesToLockController alloc] init];
    [self presentViewController:VC animated:YES completion:nil];
}

- (void) updateViewConstraints
{
    [super updateViewConstraints];
    [resultField autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [resultField autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:110];
    [resultField autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [resultField autoSetDimension:ALDimensionHeight toSize:34];
 
    [resultImage autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:100];
    [resultImage autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:100];
    [resultImage autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:resultField withOffset:20];
    [resultImage autoSetDimension:ALDimensionHeight toSize:200];
    
    [btn autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [btn autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:btn1 withOffset:-10];
    [btn autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:resultImage withOffset:20];
    [btn autoSetDimension:ALDimensionHeight toSize:44];
    
    [btn1 autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [btn1 autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:btn withOffset:10];
    [btn1 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:resultImage withOffset:20];
    [btn1 autoSetDimension:ALDimensionHeight toSize:44];
    [btn1 autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:btn];//宽相等
    
    [btn2 autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [btn2 autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [btn2 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:btn1 withOffset:20];
    [btn2 autoSetDimension:ALDimensionHeight toSize:44];
    
    [btn3 autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [btn3 autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [btn3 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:btn2 withOffset:10];
    [btn3 autoSetDimension:ALDimensionHeight toSize:44];
    
    [btn4 autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [btn4 autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [btn4 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:btn3 withOffset:10];
    [btn4 autoSetDimension:ALDimensionHeight toSize:44];
    
    [btn5 autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:50];
    [btn5 autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:50];
    [btn5 autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:btn4 withOffset:10];
    [btn5 autoSetDimension:ALDimensionHeight toSize:44];
    
}

- (void) press:(UIButton *) btn
{
    NSLog(@"pressed");
    ZBarReaderViewController *reader = [[ZBarReaderViewController alloc] init];
    reader.readerDelegate = self;
    ZBarImageScanner *scanner = reader.scanner;
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    [self presentViewController:reader animated:YES completion:nil];
}

- (void) pressAction:(UIButton *) btn
{
    resultImage.image = [QRCodeGenerator qrImageForString:resultField.text imageSize:resultImage.bounds.size.width];
}

- (void) pressAction1:(UIButton *) button
{
    if (button.tag == 201) {
        newController *new = [[newController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:new];
        [self presentViewController:nav animated:YES completion:nil];
    }
    else if (button.tag == 202) {
        testViewController *testVC = [[testViewController alloc] init];
        [self.navigationController pushViewController:testVC animated:YES];
    }
    else if (button.tag == 203) {
        MapViewController *mapVC = [[MapViewController alloc] init];
        [self.navigationController pushViewController:mapVC animated:YES];
    }
    else {
        GesturesToLockController *ges = [[GesturesToLockController alloc] init];
        [self presentViewController:ges animated:YES completion:nil];
//        [self.navigationController pushViewController:ges animated:YES];
    }
}


- (void) imagePickerController: (UIImagePickerController*) reader
 didFinishPickingMediaWithInfo: (NSDictionary*) info{
    id<NSFastEnumeration> results = [info objectForKey: ZBarReaderControllerResults];
    ZBarSymbol *symbol = nil;
    for(symbol in results)
        break;
    NSString *textd = symbol.data;
    //解决中文乱码问题
    if ([textd canBeConvertedToEncoding:NSShiftJISStringEncoding])
    {
        textd = [NSString stringWithCString:[textd cStringUsingEncoding:
                                             NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
    }
    resultText.text = [NSString stringWithFormat:@"%@",textd];
    resultImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [reader dismissViewControllerAnimated:YES completion:nil];
    
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([resultField isFirstResponder]) {
        
        [resultField resignFirstResponder];
    }
}

//- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//    
//    [picker dismissModalViewControllerAnimated: YES];
//    [self.navigationController popViewControllerAnimated:NO];
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
